﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Aop.Api;
using Aop.Api.Domain;
using Aop.Api.Request;
using Aop.Api.Response;
using BLL;

namespace yuejilalala

{
    public class orderForm 
    {
        //订单查询API
        public static void alipayTradeQuery(string ddh)
        {
            DefaultAopClient client = new DefaultAopClient(config.URL, config.APPID, config.RSA_PRIVATE_KEY, "json", "1.0", config.SIGNTYPE, config.ALIPAY_PUBLIC_KEY, config.CHARSET, false);

            // 商户订单号，和交易号不能同时为空
            string out_trade_no = ddh.Trim();


            //支付宝交易号，和商户订单号不能同时为空
            //string trade_no = WIDtrade_no.Text.Trim();

            AlipayTradeQueryModel model = new AlipayTradeQueryModel();
            model.OutTradeNo = out_trade_no;
            //model.TradeNo = trade_no;

            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
            request.SetBizModel(model);

            AlipayTradeQueryResponse response = null;
            try
            {
                response = client.Execute(request);
                HttpContext.Current.Session ["AlipayOrderNumber"] = response.TradeNo;//将支付宝订单号保存到叫AlipayOrderNumber的Session中
                HttpContext.Current.Session["orderStatusCh"] = response.TradeStatus;//订单状态详情

            }
            catch (Exception exp)
            {
                throw exp;
            }
        }

        //判定订单状态
        public static string ifOrderStatus()
        {
            OrderManager orderManager = new OrderManager();
            if (HttpContext.Current.Session["orderStatusCh"] != null)
            {
                string AlipayOrderNumber = HttpContext.Current.Session["AlipayOrderNumber"].ToString();
                string orderStatusCh = HttpContext.Current.Session["orderStatusCh"].ToString();
                if (orderStatusCh == "TRADE_SUCCESS")
                {
                    string ddh =HttpContext.Current.Session["ddh"].ToString();//接收订单号
                    orderManager.UporderByIdIsState(ddh);//执行修改
                    HttpContext.Current.Session["orderStatus"] = null;//清空Session
                    HttpContext.Current.Session["ddh"] = null;
                    HttpContext.Current.Session["orderStatusCh"] = null;
                    return "该订单已经支付";

                }
                else
                {
                    return "";
                }
               

            }
            else
            {

                return "";
            }
        }
    }
}